<?php
namespace Home\Controller;
use Common\Controller\HomeBaseController;
use Think\Verify;
use Common\Model\MemberModel;
use Think\Model;
use Common\Model\RenwuModel;
use Common\Model\TixianModel;
use Common\Model\ChongzhiModel;
use Common\Model\ArticleModel;
use Common\Model\JiluModel;

class ShuaController extends HomeBaseController {
	protected function _initialize() {
		parent::_initialize();
		if (! MID) { // 还没登录 跳转到登录页面
			if (IS_POST) $this->error('登录超时, 请重新登录');
			else $this->redirect(C('LOGIN_URL'));
		}
		$user = session('user_auth');//判断是否进行短信认证
		$member = new MemberModel();
		if($member->where("id=".MID)->getField("status") == -1){
			$this->redirect("Index/duanxin");
		}
	}
	/*
	 * 刷手任务大厅
	*/
	public function lists($cate = NULL,$xinyong = NULL){
		$renwu = new RenwuModel();
		$member = new MemberModel();
		$map = array();
		$map['status'] = array("eq",1);
		/************筛选*****************/
		if(!empty($cate)){
			$map['cate'] = $cate;
			$this->assign("cate",$cate);
		}
		$filter = I('post.');
		if($filter['xinyong']){
			$map['xinyong'] = $filter['xinyong'];
			$this->assign("xinyong",$filter['xinyong']);
		}
		if($filter['orderid']){
			$map['id'] = $filter['orderid'];
			$this->assign("orderid",$filter['orderid']);
		}
		/************筛选*****************/		
		C('LIST_ROWS',C('cfg_linerows'));
		$list = $this->_lists($renwu,$map,'createtime DESC');
		//list数据处理
		foreach ($list as &$v) {
			$v['time_type'] =date('Y-m-d H:i:s',$v['createtime']);
			$v['faburen'] = $member->where("id=".$v['sid'])->getField("account");//查询发布任务账户
			if(!empty($v['jid'])){
				$v['caozuo'] = 0;
			}else {
				$v['caozuo'] = 1;
			}
		}
		unset($v);
		$this->assign("list",$list);	
		$this->display();
	}
	/*
	 * 刷手任务详情
	*/
	public function info($id) {
		$renwu = new RenwuModel();
		$member = new MemberModel();
		$taobao = new Model('Taobao');
		
		$info = $renwu->where("id = ".$id)->find();
		if($info['cate'] == 1){$info['renwu_type'] = "垫付";	}
		elseif ($info['cate'] == 2){$info['renwu_type'] = "立返";	}
		elseif ($info['cate'] == 3){$info['renwu_type'] = "远程";	}
		elseif ($info['cate'] == 4){$info['renwu_type'] = "浏览";	}
		$info['time_type'] = date('Y-m-d H:i:s',$info['createtime']);
		$info['shangname'] = $member->where("id = ".$info['sid'])->getField("account");//商家账号
		$info['mobile'] = $member->where("id = ".$info['sid'])->getField("mobile");//商家手机号
		$info['qq'] = $member->where("id = ".$info['sid'])->getField("qq");//商家qq号
		//订单状态
		if ($info['status'] == 1){	$info['zhuangtai'] = "等待接单";	}
		elseif ($info['status'] == 2){	$info['zhuangtai'] = "客服审核";	}
		elseif ($info['status'] == 3){	$info['zhuangtai'] = "任务进行中";	}
		elseif ($info['status'] == 4){	$info['zhuangtai'] = "申请放款";	}
		elseif ($info['status'] == 5){	$info['zhuangtai'] = "任务完成";	}
		elseif ($info['status'] == 6){	$info['zhuangtai'] = "纠纷中";	}
		elseif ($info['status'] == 7){	$info['zhuangtai'] = "拒绝放单";	}
		elseif ($info['status'] == 8){	$info['zhuangtai'] = "刷手取消";	}
		
		//判断用户是否已经接单
		if($info['jid'] == MID){
			$this->assign("yj",1);
		}else {//查询当前登录用户的淘宝账号
			$this->tblist = $taobao->where("status = 1 and sid = ".MID)->select();
		}
		$this->assign("info",$info);
		$this->display();
	}
	/*
	 * 刷手接单(做任务表添加记录)
	 */
	public function jiedan(){
		$user = session('user_auth');
		$member = new MemberModel();
		$renwu = new RenwuModel();

		if($member->where("id=".MID)->getField("status") == 0){
			$this->redirect("Shua/renzheng");
		}

		if(IS_POST){
			$data = I('post.');
			$map = array();
			$map['taobao'] = $data['taouser'];
			$map['status'] = 2;
			$map['jid'] = MID;
			$map['jdtime'] = time();
			if(false === $renwu->where("id = ".$data['id'])->save($map)){
				$this->error("接任务申请失败");
			}else{
				$this->success("接任务申请成功");
			}
		}
	}
	/*
	 * 验证商品地址
	 */
	public function yanzheng(){
		if (IS_POST){
			$filter = I('post.');
			$url = $filter['url'];
			$id = $filter['id'];
			$renwu = new RenwuModel();
			if($url == $renwu->where("id=".$id)->getField("url")){
				$this->success("验证成功");
			}else{$this->error("验证失败");}
		}
	}
	/*
	 * 刷手我的任务
	*/
	public function mylists($flag,$keyid = NULL,$status = 1,$keyrowds = NULL){
		$renwu = new RenwuModel();
		$member = new MemberModel();
		$map = array();
		$map['jid'] = MID;
		if(!empty($status) && $status != 1){
			$map['status'] = $status;
			$this->assign("status",$status);
		}
		$list = $renwu->where($map)->select();
		//list数据处理
		foreach ($list as &$v) {
			$v['time_type'] =date('Y-m-d H:i:s',$v['updatetime']);
			$v['faburen'] = $member->where("id=".$v['sid'])->getField("account");//查询发布任务账户
			if($v['cate'] == 1){
				$v['leixing'] = "垫付单";
			}elseif ($v['cate'] == 2){
				$v['leixing'] = "立返单";
			}elseif ($v['cate'] == 3){
				$v['leixing'] = "远程单";
			}elseif ($v['cate'] == 4){
				$v['leixing'] = "浏览单";
			}
			//订单状态
			if ($v['status'] == 1){	$v['zhuangtai'] = "等待接单";	}
			elseif ($v['status'] == 2){	$v['zhuangtai'] = "客服审核";	}
			elseif ($v['status'] == 3){	$v['zhuangtai'] = "任务进行中";	}
			elseif ($v['status'] == 4){	$v['zhuangtai'] = "申请放款";	}
			elseif ($v['status'] == 5){	$v['zhuangtai'] = "任务完成";	}
			elseif ($v['status'] == 6){	$v['zhuangtai'] = "纠纷中";	}
			elseif ($v['status'] == 7){	$v['zhuangtai'] = "拒绝放单";	}
			elseif ($v['status'] == 8){	$v['zhuangtai'] = "刷手取消";	}
		}
		unset($v);
		$this->assign("list",$list);
		$this->display($flag);
	}
	/*
	 * 刷手账户信息
	 */
	public function userinfo() {
		if(IS_POST){
			$member = new MemberModel();
			$data = I('post.');
			if($member->where("id = ".MID)->save($data)){
				$this->success("成功");
			}else {	$this->error("失败");}
		}else {
			//账户信息
			$member = new MemberModel();
			$this->info = $member->where("id = ".MID)->find();
			$this->display();
		}
	}
	/*
	 * 刷手账户登录密码
	 */
	public function usermima() {
		if(IS_POST){
			$member = new MemberModel();
			$m = $member->where("id = ".MID)->getField("password");
			$data = I('post.');
			if(pwd_hash($data['ypassword']) != $m){
				$this->error("失败");
			}
			if($member->where("id = ".MID)->setField("password",pwd_hash($data['npassword']))){
				$this->success("成功");
			}			
		}else{
			//账户信息
			$member = new MemberModel();
			$this->info = $member->where("id = ".MID)->find();
			$this->display();
		}
	}
	/*
	 * 刷手交易密码
	 */
	public function jiaoyimima() {
		if(IS_POST){
			$member = new MemberModel();
			$m = $member->where("id = ".MID)->getField("password");
			$data = I('post.');
			if(pwd_hash($data['dpassword']) != $m){
				$this->error("失败");
			}
			if($member->where("id = ".MID)->setField("jpassword",pwd_hash($data['jpassword']))){
				$this->success("成功");
			}			
		}else{
			//账户信息
			$member = new MemberModel();
			$this->info = $member->where("id = ".MID)->find();
			$this->display();
		}
	}
	/*
	 * 商家体现
	 * 1、可用余额减少对应数值
	 * 2、冻结金额增加对应数值
	 * 3、增加体现记录
	 */
	public function tixian() {
		$jilu = new JiluModel();
		if(IS_POST){
			$member = new MemberModel();
			$jilu = new JiluModel();
			$m = $member->where("id = ".MID)->getField("jpassword");
			$data = I('post.');
			if(pwd_hash($data['jpassword']) != $m){
				$this->error("交易密码错误");
			}
						
			$model = new Model();
			$model->startTrans();
			
			if ($data['money'] > $member->where("id = ".MID)->getField("able_money")){//判断提现金额是否大约可用余额
				$model->rollback();
				$this->error("提现金额大约可用余额！");
			}
			
			if(false === $member->where("id = ".MID)->setDec("able_money",$data['money'])){//减少可用余额
				$model->rollback();
				$this->error("减少可用余额失败！");
			}
			
			if(false === $member->where("id = ".MID)->setInc("ice_money",$data['money'])){//增加冻结金额
				$model->rollback();
				$this->error("增加冻结金额失败！");
			}
			
			$map = array();
			$map['sid'] = MID;
			$map['money'] = $data['money'];
			$map['xingming'] = $data['realname'];
			$map['zhifubao'] = $data['zhifubao'];
			$map['createtime'] = time();
			$map['cate'] = 2;
			$map['status'] = 0;
			$map['remark'] = "用户提现";
			$map['yue'] = $member->where("id = ".MID)->getField("able_money");
			if(false === $jilu->add($map)){//添加提现记录
				$model->rollback();
				$this->error("增加提现记录失败！");
			}	
			
			$model->commit();
			$this->success("体现申请提交成功！");
		}else{
			//提现列表
			$list = $jilu->where("cate = 2 and sid = ".MID)->select();
			//list数据处理
			foreach ($list as &$v) {
				$v['time_type'] =date('Y-m-d H:i:s',$v['createtime']);
			}
			unset($v);
			$this->assign("list",$list);
			//账户信息
			$member = new MemberModel();
			$this->info = $member->where("id = ".MID)->find();
			$this->display();
		}
	}
	/*
	 * 刷手个人资金
	*/
	public function userzijin() {
		$jilu = new JiluModel();
		//资金明细
		$zjlist = $jilu->where("sid = ".MID)->select();
		//list数据处理
		foreach ($zjlist as &$v) {
			$v['time_type'] =date('Y-m-d H:i:s',$v['createtime']);
		}
		unset($v);
		$this->assign("zjlist",$zjlist);
		$this->display();
	}	
	/*
	 * 刷手淘宝
	 */
	public function taobao(){
		$taobao = new Model('taobao');
		if (IS_POST){
			$filter = I('post.');
			$data['taobao'] = $filter['taobaoname'];
			$data['sid'] = MID;
			$data['createtime'] = time();
			if(false == $taobao->add($data)){
				$this->error("失败");
			}else{
				$this->success("成功");
			}
		}else{
			$this->tbnum = $taobao->where("status >0 and sid=".MID)->count();//绑定的淘宝号数量
			$taobao = $taobao->where("status > 0  and sid=".MID)->select();
			//list数据处理
			foreach ($taobao as &$v) {
				$v['time_type'] =date('Y-m-d H:i:s',$v['createtime']);
			}
			unset($v);
			$this->assign("taobao",$taobao);
			$this->display();
		}		
	}
	/*
	 * 删除淘宝账号
	 */
	public function taobaodel($id){
		$taobao = new Model('taobao');
		if (false == $taobao->where("id=".$id)->setField("status",-1)){
			$this->error("失败");
		}else{	$this->success("成功");}
	}
	/*
	 * 刷手实名认证
	 */
	public function renzheng(){
		$member = new MemberModel();
		$model = new Model();
		$jilu = new JiluModel();
		if(IS_POST){
			$data = array();
			$data['id'] = MID;
			$data['realname'] = I('post.realname');
			$data['idcard'] = I('post.cardid');
			$data['zhengmian'] = $_FILES['zhengmian'];
			$data['beimian'] = $_FILES['beimian'];

			if (empty($_FILES['zhengmian']) || $data['zhengmian']['error']=='4') {
				unset($data['zhengmian']);
			}
			if (empty($_FILES['beimian']) || $data['beimian']['error']=='4') {
				unset($data['beimian']);
			}
			if (false === $member->myRenzheng($data)) {
				$this->error("提交失败");
			}else {
				$this->success("提交成功",U('Shua/index'));
			}
		}else {
			$this->info=$member->where("id=".MID)->find();
			$this->display();
		}
	}
	/*
	 * 上传审核图
	 */
	public function up(){
		$renwu = new RenwuModel();
		if(IS_POST){
			$data = array();
			$data['id'] = I('post.id');
			$data['ipimg'] = $_FILES['ipimg'];
			$data['qingchuimg'] = $_FILES['qingchuimg'];
			$data['goumaiimg'] = $_FILES['goumaiimg'];
			$data['pinglunimg'] = $_FILES['pinglunimg'];
				
			if (empty($_FILES['ipimg']) || $data['ipimg']['error']=='4') {
				unset($data['ipimg']);
			}
			if (empty($_FILES['qingchuimg']) || $data['qingchuimg']['error']=='4') {
				unset($data['qingchuimg']);
			}
			if (empty($_FILES['goumaiimg']) || $data['goumaiimg']['error']=='4') {
				unset($data['goumaiimg']);
			}
			if (empty($_FILES['pinglunimg']) || $data['pinglunimg']['error']=='4') {
				unset($data['pinglunimg']);
			}
			if (false === $renwu->myUp($data)) {
				$this->error("上传失败");
			}else {
				$this->success("提交成功");
			}
		}
	}
}